package com.cwd.arm.inter.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwd.arm.application.dto.user.UserManageDto;
import com.cwd.arm.application.service.read.facade.UserReadService;
import com.cwd.arm.application.service.write.facade.UserWriteService;
import com.cwd.arm.common.dto.session.UserDto;
import com.cwd.arm.common.util.SessionUtil;
import com.cwd.arm.inter.query.UserQuery;
import com.cwd.arm.inter.util.DtoToVoUtil;
import com.cwd.arm.inter.util.PageDtoToVoUtil;
import com.cwd.arm.inter.util.QueryToDaoQueryUtil;
import com.cwd.arm.inter.util.VoToDtoUtil;
import com.cwd.arm.inter.vo.PageVo;
import com.cwd.arm.inter.vo.ResponseVo;
import com.cwd.arm.inter.vo.user.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by yan on 21-11-11.
 */
@Api(value = "用户接口", description = "用户接口")
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserReadService readService;

    @Autowired
    private UserWriteService userWriteService;

    @ApiOperation(value = "查询用户信息", notes = "查询用户信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户id",required = true,dataType = "Long", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseVo<UserVo> getUser(@PathVariable Long id) {
        UserDto dto = readService.getUserById(id);
        return ResponseVo.<UserVo>BUILDER().success(DtoToVoUtil.convertToUserVo(dto));
    }

    @ApiOperation(value = "查询用户信息列表", notes = "查询用户信息列表")
    @GetMapping("/page")
    public ResponseVo<PageVo<UserManageVo>> getUserPage(UserQuery query) {
        Page<UserManageDto> dtoPage = readService.selectPage(QueryToDaoQueryUtil.toDaoQuery(query));
        PageVo<UserManageVo> pageVo = new PageVo<>();
        pageVo.setRecordList(DtoToVoUtil.convertToUserManageVoList(dtoPage.getRecords()));
        return ResponseVo.<PageVo<UserManageVo>>BUILDER().success(PageDtoToVoUtil.toVo(dtoPage,pageVo));
    }

    @ApiOperation(value = "查询所有用户信息列表", notes = "查询所有用户信息列表")
    @GetMapping("")
    public ResponseVo<List<UserManageVo>> getUserList(UserQuery query) {
        return ResponseVo.<List<UserManageVo>>BUILDER().success(null);
    }

    @ApiOperation(value = "新增用户信息", notes = "新增用户信息")
    @Secured({"ROLE_USER_ADD"})
    @PostMapping("")
    public ResponseVo<String> add(@RequestBody UserAddVo addVo) {
        userWriteService.save(VoToDtoUtil.convertToDto(addVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "修改用户信息", notes = "修改用户信息")
    @Secured({"ROLE_USER_EDIT"})
    @PutMapping("")
    public ResponseVo<String> update(@RequestBody UserUpdateVo updateVo) {
        userWriteService.update(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "用户id",required = true,dataType = "Long", paramType = "path")
    })
    @Secured({"ROLE_USER_DEL"})
    @DeleteMapping("/{id}")
    public ResponseVo<String> del(@PathVariable Long id) {
        userWriteService.del(id);
        return ResponseVo.<String>BUILDER().success();
    }

    @ApiOperation(value = "重置用户密码", notes = "重置用户密码")
    @Secured({"ROLE_USER_RESET"})
    @PostMapping("resetPassword")
    public ResponseVo<String> resetPassword(@RequestBody UserResetPasswordVo updateVo) {
        userWriteService.update(VoToDtoUtil.convertToDto(updateVo));
        return ResponseVo.<String>BUILDER().success(null,"密码重置成功默认密码:"+updateVo.getPassword());
    }

}
